<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

/**--tmpl-use--**/

class SinglepageCateAction extends CommonAction {

	protected $serviceBean="information.informationSinglepageCate.service";
	protected $caseService="information.informationSinglepage.service";

	public function C_start()
	{
		parent::C_start();
	}

	public function add(HttpRequest $request) {

		parent::add($request);
		$parents = Beans::get($this->serviceBean)->getParents(0);
		$this->assign("admin_name", adminName());
		$this->assign("parents", $parents);
        $this->assign("title", "添加单页类型");
        $this->assign('bread', array('信息管理','单页类型列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/singlepageCate/index'),'javascript:;'));
	
	}
	

	public function edit(HttpRequest $request) {
		parent::edit($request);
		$id = $request->getParameter('id');
		$parents = Beans::get($this->serviceBean)->getParents($id);
		$this->assign("admin_name", adminName());
		$this->assign("parents", $parents);
        $this->assign("title", "编辑单页类型");
        $this->assign('bread', array('信息管理','单页类型列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/singlepageCate/index'),'javascript:;'));
	}

	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','单页类型表');
		$s_name = $request->getParameter('s_name',"urldecode");
		$this->assign('s_name',$s_name);
		if($s_name !== '' && $s_name!== NULL)$conditions['name'] = '%'.$s_name.'%';
		$s_parent_id = $request->getParameter('s_parent_id');
		$this->assign('s_parent_id',$s_parent_id);
		if($s_parent_id !== '' && $s_parent_id!== NULL)$conditions['parent_id'] = '%'.$s_parent_id.'%';
		$s_summary = $request->getParameter('s_summary',"urldecode");
		$this->assign('s_summary',$s_summary);
		if($s_summary !== '' && $s_summary!== NULL)$conditions['summary'] = '%'.$s_summary.'%';
		$s_manager_id = $request->getParameter('s_manager_id');
		$this->assign('s_manager_id',$s_manager_id);
		if($s_manager_id !== '' && $s_manager_id!== NULL)$conditions['manager_id'] = '%'.$s_manager_id.'%';
		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->assign('_filter',array('name','information_singlepage_cate_id','summary','admin_manager_id','app_id'));
		$this->assign('_header',array('','上级分类','分类描述','','AppId'));
		$this->setConditions($conditions);
	
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		//parent::index($request);

		$service = Beans::get($this->getServiceBean());
		$total = $service->count($this->getConditions());
		$items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
				array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
		$this->assign('pagesize', $this->getPagesize());
		$this->assign('items', $items);
		$this->generatePageMenu($total);

		if( $request -> getParameter('export') == 1 ){
			$ids = $request -> getParameter('ids');
			if( $ids ){
				$conditions['id']['$in']=$ids;
				$this -> setConditions($conditions);
			}
			$this->setPage(1);
			$this->setPagesize(2147483648);
			$items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
					array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
			$excel = array();
			foreach($items as $k => $v)
			{
				$tmp = array();
				$tmp[] = $v["name"];
				$tmp[] = $v["parent"];
				$tmp[] = $v["summary"];
				$tmp[] = $v["admin_manager_username"];
				$tmp[] = $v["sort_index"];
				$excel[] = $tmp;
			}
			$header = array('类型名称','上级分类','类型描述','操作人','排序');
			phpexcel("单页列表",$header,$excel);
			exit();
		}

		$this->assign("singlepage", "/admin/singlepage/index");
		$this->assign("title", "单页类型表列表");
        $this->assign('bread', array('信息管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/index');
	}

	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		$data['manager_id'] = adminId();
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}

	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}
	public function delete(HttpRequest $request)
	{
		parent::deleteCate($request,$this->caseService);
	}
	public function deletes(HttpRequest $request)
	{
		parent::deletesCate($request,$this->caseService);
	}
	

}
